<template>
    <div id="div1">
        <el-form  :model="user" label-width="80px">
           <el-form-item label="用户名">
               <el-input v-model="user.name"></el-input>
            </el-form-item>
            <el-form-item label="密码">
               <el-input v-model="user.password" type="password"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio v-model="user.gender" label="男">男</el-radio>
              <el-radio v-model="user.gender" label="女">女</el-radio>
            </el-form-item>
            <el-form-item label="年龄">
               <el-input v-model="user.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="regist">注册</el-button>
                <el-button @click="toLogin">登录</el-button>
            </el-form-item>

        </el-form>
    </div>
</template>
<style scoped>
   #div1{
     width: 400px;
     margin: 0 auto;
   }
</style>



<script>
import axios from "axios";

export default {
  data: function() {
    return {
      user: { "name": "", "password": "","gender":"女", "age":""}
    };
  },
  methods: {
    regist: function() {
      var that=this;
      /*
      *axios 把数据传给后台
      */
      axios.post("/api/user/add", this.user).then(function(res) {
        var data = res.data;
        if (data.code == 200) {
           that.$router.push({
            path: "/login"
          });
        } else {
          alert(data.msg);
        }
      });
    },
    //去往登录页面
    toLogin:function(){
       var that=this;
        that.$router.push({
            path: "/login"
          });
    }
  }
};
</script>

